import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

from sklearn.datasets import make_s_curve
#
# ## 数据
# x, t = make_s_curve(2000, 0.1, random_state=42)
# ## 降维的维数
# d1 = 2
# ## 距离矩阵
# D = np.zeros(shape=(len(x), len(x)), dtype=np.float64)
# for i in range(len(x) - 1):
#     for j in range(i + 1, len(x)):
#         D[i, j] = distance(x[i], x[j])
#         D[j, i] = D[i, j]
#
# # 计算D的列、行、整体均值
# dist_i = D.mean(axis=1)  # 行
# dist_j = D.mean(axis=0)  # 列
# dist_avg = D.mean()
#
# # 初始化矩阵B
# B = np.empty(shape=D.shape, dtype=np.float64)
# # 得到B
# for i in range(len(B)):
#     for j in range(len(B)):
#         B[i, j] = -(D[i, j] ** 2 - dist_i[i] ** 2 - dist_j[j] ** 2 + dist_avg ** 2) / 2
#
# # 求B的特征值和特征向量
# val, vec = np.linalg.eig(B)
# # 中间矩阵
# m1 = np.diag(val[np.argsort(val)[-1:-3:-1]].real)
# m1 = np.sqrt(m1)
# m2 = vec[:, np.argsort(val)[-1:-3:-1]].real
# # 降维后的矩阵
# rs = np.dot(m1, m2.T).T

from sklearn import manifold
x, t = make_s_curve(2000, 0.1, random_state=42)
mds = manifold.MDS(n_components=2, max_iter=100, n_init=1)  #建立MDS模型
y = mds.fit_transform(x) # 训练并返回结果

print(x)

